<?php

namespace app\beautiful\controller\admin\appointment;

use app\BaseController;

use app\beautiful\model\appointment\AppointmentSettingModel;



class Setting extends BaseController{

    public function show()

    {

        if(null === $setting= AppointmentSettingModel::where([['shop_id','=',$this->request->shopId]])->find()) return $this->success([]);

        $appoint_time=[$setting->start_time,$setting->end_time];

        $form=[

            'week'=>$setting->week,

            'appoint_time'=>$appoint_time,

            'appointment_num'=>$setting->appointment_num,

            'appointment_interval'=>$setting->appointment_interval,

            'appointment_advance'=>$setting->appointment_advance,

            'check_member_project'=>$setting->check_member_project,

            'rest_notice'=>$setting->rest_notice,

        ];



        return $this->success(['form'=>$form]);

    }



    public function save()

    {

        $today=date("Y-m-d",$this->request->time());

        $appoint_time=$this->request->param('appoint_time/a',[]);

        $week=$this->request->param('week/a',[]);

        $appointment_num=$this->request->param('appointment_num/d',0);

        $appointment_advance=$this->request->param('appointment_advance/d',0);

        $appointment_interval=$this->request->param('appointment_interval/d',0);

        $check_member_project=$this->request->param('check_member_project/d',0);

        $rest_notice=$this->request->param('rest_notice');



        if(empty($appoint_time) || !is_array($appoint_time) ||count($appoint_time)!=2) return $this->error('请选择预约时间');

        $start_time=$appoint_time[0];$end_time=$appoint_time[1];

        if(false === $start_time_unix=strtotime($today.' '.$start_time.':00')) return $this->error('请选择预约开始时间');

        if(false === $end_time_unix=strtotime($today.' '.$end_time.':00')) return $this->error('请选择预约结束时间');

        $interval=$end_time_unix-$start_time_unix;

        if($interval<=0) return $this->error('预约开始时间不能大于结束时间');

        if($interval%1800!=0)return $this->error('预约时间间隔必须是30分钟的整倍');

        if(empty($week) || !is_array($week)) return $this->error('请选择使用星期');

        if(empty($appointment_num) || $appointment_num <= 0) return $this->error('请输入同时段预约数，且不能小于0');

        if(empty($appointment_interval) || !in_array($appointment_interval,[30,60,90,120])) return $this->error('请选择正确的间隔');

        if(empty($appointment_advance) || $appointment_advance<0) return $this->error('请输入正确的预约提前小时数');

        if(empty($check_member_project) || !in_array($check_member_project,[0,1])) $check_member_project=0;

        if(empty($rest_notice)) return $this->error('请输入休息公告');



        if(null === $setting= AppointmentSettingModel::where([['shop_id','=',$this->request->shopId]])->find()){

            $setting = new AppointmentSettingModel();

            $setting->shop_id=$this->request->shopId;

        }



        $setting->start_time=$start_time;

        $setting->end_time=$end_time;

        $setting->week=$week;

        $setting->appointment_num=$appointment_num;

        $setting->appointment_interval=$appointment_interval;

        $setting->appointment_advance=$appointment_advance;

        $setting->check_member_project=$check_member_project;

        $setting->rest_notice=$rest_notice;

        $setting->save();



        return $this->success('保存成功');



    }

}

